package com.deke.utils;


import com.deke.vo.PageVo;

import java.util.List;

/**
 * @author ：Username 刘亦辰（59372312@qq.com）
 * @date ：Created in 2022/12/22 14:27
 * @description：特殊分页处理
 * @modified By：
 * @version:
 **/
public class ListPage<T> {
    private Integer pageNum;
    private Integer pageSize;
    private List<T> list;

    public static PageVo startPage(List list, int pageNum, int pageSize) {
        if (list == null || list.size() == 0) {
            return null;
        }

        int count = list.size(); // 记录总数
        int pageCount = 0; // 页数
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }
        if (pageNum > pageCount) {
            return null;
        }
        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引

        if (pageNum != pageCount) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }
        if (fromIndex > count || toIndex > count) {
            return null;
        }
        return new PageVo(count,pageSize,list.subList(fromIndex, toIndex));
    }
}